package cn.net.chenbao.medical.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

import cn.net.chenbao.medical.R;
import cn.net.chenbao.medical.adapter.radapter.CommonViewHolder;
import cn.net.chenbao.medical.adapter.radapter.RvCommonAdapter;
import cn.net.chenbao.medical.bean.Patient;
import cn.net.chenbao.medical.util.MyViewUtils;
import cn.net.chenbao.medical.util.TimeUtil;

/**
 * Created by 木头 on 2018/2/1.
 */

public class PatientAdapter extends RvCommonAdapter<Patient> {
    private Context mContext;

    public PatientAdapter(Context mContext, List<Patient> mdatas, int mLayoutId) {
        super(mContext, mdatas, mLayoutId);
        this.mContext = mContext;
    }

    @Override
    public void convert(CommonViewHolder holder, Patient patient) {
        CheckBox cb = (CheckBox) holder.getView(R.id.cb);
        if (!TextUtils.isEmpty(patient.PyFlag) && TextUtils.equals("1", patient.PyFlag)) {
            cb.setChecked(true);
        } else {
            cb.setChecked(false);
        }
        holder.setText(R.id.tv_type, String.format(mContext.getString(R.string.name_type), patient.Printid + "", patient.PrintRow == null ? "" : patient.PrintRow));
        holder.setText(R.id.tv_ypsm, patient.Ypsm == null ? "" : patient.Ypsm);
        holder.setText(R.id.sickness_name, patient.KsmcName == null ? "" : patient.KsmcName);
        holder.setText(R.id.tv_bed_number, patient.Cw == null ? "" : patient.Cw);
        holder.setText(R.id.tv_drug_method, String.format(mContext.getString(R.string.method), patient.YfName == null ? "" : patient.YfName, patient.YfId == null ? "" : patient.YfId));
        holder.setText(R.id.tv_speed, String.format(mContext.getString(R.string.speed), patient.Syds1 == null ? "" : patient.Syds1));
        holder.setText(R.id.tv_number, patient.ComNo == null ? "" : patient.ComNo);
        holder.setText(R.id.tv_row, patient.PrintRow == null ? "" : patient.PrintRow);
        holder.setText(R.id.tv_patient_name, patient.Xm == null ? "" : patient.Xm);
        holder.setText(R.id.tv_patient_age, patient.Age == null ? "" : patient.Age);

        holder.setText(R.id.tv_patient_bunber, patient.Zyh == null ? "" : patient.Zyh);
        holder.setText(R.id.tv_check_print, patient.Sfdy == null ? "" : patient.Sfdy);
        holder.setText(R.id.tv_jc, patient.JcOperName == null ? "" : patient.JcOperName);
        holder.setText(R.id.tv_pz, patient.RealOperName == null ? "" : patient.RealOperName);
        holder.setText(R.id.tv_cc, patient.OutOperName == null ? "" : patient.OutOperName);
        holder.setText(R.id.tv_jj, patient.HsjjOperName == null ? "" : patient.HsjjOperName);
        holder.setText(R.id.tv_patient_time, String.format(mContext.getString(R.string.name_time), TimeUtil.timeToData(patient.Rq)));
        LinearLayout llDrugContainer = (LinearLayout) holder.getView(R.id.ll_drug_container);
        llDrugContainer.removeAllViews();
        try {
            ShowData(llDrugContainer, patient);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private void ShowData(LinearLayout llDrugContainer, Patient patient) throws Exception {
        if (!TextUtils.isEmpty(patient.Yp1)) {
            View view = View.inflate(mContext, R.layout.drug_information_item, null);
            TextView tvDrugName = view.findViewById(R.id.tv_item_drug_name);
            TextView tvDrugSpecifition = view.findViewById(R.id.tv_item_drug_specification);//规格
            TextView tvDrugNumber = view.findViewById(R.id.tv_item_number);
            TextView tvDrugDosage = view.findViewById(R.id.tv_item_drug_dosage);//用量
            TextView tvDrugUnit = view.findViewById(R.id.tv_item_unit);
            TextView tvDrugBatchNumber = view.findViewById(R.id.tv_batch_number);
            TextView tvDrugValidity = view.findViewById(R.id.tv_validity);
            tvDrugName.setText(patient.Yp1);
            tvDrugSpecifition.setText("" + patient.Jl1);
            tvDrugNumber.setText("" + MyViewUtils.numberFormatWithOne(patient.Jlyl1 / patient.Jl1));
            tvDrugDosage.setText("" + patient.Jlyl1);
            tvDrugUnit.setText(patient.JldwName1 == null ? "" : patient.JldwName1);

            tvDrugBatchNumber.setText(String.format(mContext.getResources().getString(R.string.batch_number), patient.PhYp1 == null ? "" : patient.PhYp1));
            tvDrugValidity.setText(String.format(mContext.getResources().getString(R.string.validity), patient.VaildDate1 == null ? "" : TimeUtil.timeToData(patient.VaildDate1)));
            llDrugContainer.addView(view);
        }
        if (!TextUtils.isEmpty(patient.Yp2)) {
            View view = View.inflate(mContext, R.layout.drug_information_item, null);
            TextView tvDrugName = view.findViewById(R.id.tv_item_drug_name);
            TextView tvDrugSpecifition = view.findViewById(R.id.tv_item_drug_specification);//规格
            TextView tvDrugNumber = view.findViewById(R.id.tv_item_number);
            TextView tvDrugDosage = view.findViewById(R.id.tv_item_drug_dosage);//用量
            TextView tvDrugUnit = view.findViewById(R.id.tv_item_unit);
            TextView tvDrugBatchNumber = view.findViewById(R.id.tv_batch_number);
            TextView tvDrugValidity = view.findViewById(R.id.tv_validity);
            tvDrugName.setText(patient.Yp2);
            tvDrugSpecifition.setText("" + patient.Jl2);
            tvDrugNumber.setText("" + MyViewUtils.numberFormatWithOne(patient.Jlyl2 / patient.Jl2));
            tvDrugDosage.setText("" + patient.Jlyl2);
            tvDrugUnit.setText(patient.JldwName2 == null ? "" : patient.JldwName2);
            tvDrugBatchNumber.setText(String.format(mContext.getResources().getString(R.string.batch_number), patient.PhYp2 == null ? "" : patient.PhYp2));
            tvDrugValidity.setText(String.format(mContext.getResources().getString(R.string.validity), patient.VaildDate2 == null ? "" : TimeUtil.timeToData(patient.VaildDate2)));
            llDrugContainer.addView(view);
        }
        if (!TextUtils.isEmpty(patient.Yp3)) {
            View view = View.inflate(mContext, R.layout.drug_information_item, null);
            TextView tvDrugName = view.findViewById(R.id.tv_item_drug_name);
            TextView tvDrugSpecifition = view.findViewById(R.id.tv_item_drug_specification);//规格
            TextView tvDrugNumber = view.findViewById(R.id.tv_item_number);
            TextView tvDrugDosage = view.findViewById(R.id.tv_item_drug_dosage);//用量
            TextView tvDrugUnit = view.findViewById(R.id.tv_item_unit);
            TextView tvDrugBatchNumber = view.findViewById(R.id.tv_batch_number);
            TextView tvDrugValidity = view.findViewById(R.id.tv_validity);
            tvDrugName.setText(patient.Yp3);
            tvDrugSpecifition.setText("" + patient.Jl3);
            tvDrugNumber.setText("" + MyViewUtils.numberFormatWithOne(patient.Jlyl3 / patient.Jl3));
            tvDrugDosage.setText("" + patient.Jlyl3);
            tvDrugUnit.setText(patient.JldwName3 == null ? "" : patient.JldwName3);

            tvDrugBatchNumber.setText(String.format(mContext.getResources().getString(R.string.batch_number), patient.PhYp3 == null ? "" : patient.PhYp3));

            tvDrugValidity.setText(String.format(mContext.getResources().getString(R.string.validity), patient.VaildDate3 == null ? "" : TimeUtil.timeToData(patient.VaildDate3)));
            llDrugContainer.addView(view);
        }
        if (!TextUtils.isEmpty(patient.Yp4)) {
            View view = View.inflate(mContext, R.layout.drug_information_item, null);
            TextView tvDrugName = view.findViewById(R.id.tv_item_drug_name);
            TextView tvDrugSpecifition = view.findViewById(R.id.tv_item_drug_specification);//规格
            TextView tvDrugNumber = view.findViewById(R.id.tv_item_number);
            TextView tvDrugDosage = view.findViewById(R.id.tv_item_drug_dosage);//用量
            TextView tvDrugUnit = view.findViewById(R.id.tv_item_unit);
            TextView tvDrugBatchNumber = view.findViewById(R.id.tv_batch_number);
            TextView tvDrugValidity = view.findViewById(R.id.tv_validity);
            tvDrugName.setText(patient.Yp4);
            tvDrugSpecifition.setText("" + patient.Jl4);
            tvDrugNumber.setText("" + MyViewUtils.numberFormatWithOne(patient.Jlyl4 / patient.Jl4));
            tvDrugDosage.setText("" + patient.Jlyl4);
            tvDrugUnit.setText(patient.JldwName4 == null ? "" : patient.JldwName4);

            tvDrugBatchNumber.setText(String.format(mContext.getResources().getString(R.string.batch_number), patient.PhYp4 == null ? "" : patient.PhYp4));

            tvDrugValidity.setText(String.format(mContext.getResources().getString(R.string.validity), patient.VaildDate4 == null ? "" : TimeUtil.timeToData(patient.VaildDate4)));
            llDrugContainer.addView(view);
        }
        if (!TextUtils.isEmpty(patient.Yp5)) {
            View view = View.inflate(mContext, R.layout.drug_information_item, null);
            TextView tvDrugName = view.findViewById(R.id.tv_item_drug_name);
            TextView tvDrugSpecifition = view.findViewById(R.id.tv_item_drug_specification);//规格
            TextView tvDrugNumber = view.findViewById(R.id.tv_item_number);
            TextView tvDrugDosage = view.findViewById(R.id.tv_item_drug_dosage);//用量
            TextView tvDrugUnit = view.findViewById(R.id.tv_item_unit);
            TextView tvDrugBatchNumber = view.findViewById(R.id.tv_batch_number);
            TextView tvDrugValidity = view.findViewById(R.id.tv_validity);
            tvDrugName.setText(patient.Yp5);
            tvDrugSpecifition.setText("" + patient.Jl5);
            tvDrugNumber.setText("" + MyViewUtils.numberFormatWithOne(patient.Jlyl5 / patient.Jl5));
            tvDrugDosage.setText("" + patient.Jlyl5);
            tvDrugUnit.setText(patient.JldwName5 == null ? "" : patient.JldwName5);
            tvDrugBatchNumber.setText(String.format(mContext.getResources().getString(R.string.batch_number), patient.PhYp5 == null ? "" : patient.PhYp5));
            tvDrugValidity.setText(String.format(mContext.getResources().getString(R.string.validity), patient.VaildDate5 == null ? "" : TimeUtil.timeToData(patient.VaildDate5)));
            llDrugContainer.addView(view);
        }
    }
}
